package cn.xm.dictionary.controller;

import cn.xm.basic.result.AjaxResult;
import cn.xm.dictionary.domain.SystemDictionaryDetail;

import cn.xm.dictionary.query.SystemDictionaryDetailQuery;

import cn.xm.dictionary.service.SystemDictionaryDetailService;

import cn.xm.basic.result.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys_dictiondetail")
public class SystemDictionaryDetailController {

    @Autowired
    private SystemDictionaryDetailService systemDictionaryDetailService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<SystemDictionaryDetail> findAll() {
        return systemDictionaryDetailService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "凭id查询")
    public SystemDictionaryDetail findById(@PathVariable("id") Long id) {
        return systemDictionaryDetailService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        systemDictionaryDetailService.delete(id);
        return AjaxResult.me();
    }

    @PutMapping("/save")
    @ApiOperation(value = "新增或修改")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        if (systemDictionaryDetail.getId() == null) {
            systemDictionaryDetailService.add(systemDictionaryDetail);
        } else {
            systemDictionaryDetailService.update(systemDictionaryDetail);
        }
        return AjaxResult.me();
    }

    @PostMapping("/query")
    @ApiOperation(value = "分页查询")
    public PageList<SystemDictionaryDetail> queryByPage(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery) {
        return systemDictionaryDetailService.queryByPage(systemDictionaryDetailQuery);

    }

    @PatchMapping("/batchdel")
    public AjaxResult batchdelete(@RequestBody List<Long> ids) {
        systemDictionaryDetailService.batchDelete(ids);
        return new AjaxResult().me();
    }

    @GetMapping("/by_type/{id}")
    public List<SystemDictionaryDetail> findByTypeId(@PathVariable("id") Long id) {
        return systemDictionaryDetailService.findByTypeId(id);
    }

}
